import React, { Component } from 'react'
import { Form, Select, Input } from 'antd'
import PropTypes from 'prop-types'

const { Option } = Select;
// 添加分类的form组件
export default class AddForm extends Component {

  static propTypes = {
    categoryList: PropTypes.array,
    parentID: PropTypes.string,
    setName: PropTypes.func,
    setId: PropTypes.func,
    defaultAddValue: PropTypes.string
  }
  changeName = (event) => {
    this.props.setName(event.target.value)
  }
  changeParentID = (value) => {
    this.props.setId(value)
  }
  render() {
    const { categoryList, parentID, defaultAddValue } = this.props
    return (
      <Form>
        <Form.Item>
          <Select 
            defaultValue={parentID}
            onChange={this.changeParentID}>
            <Option value='0'>一级分类</Option>
            {
              categoryList.map(c => <Option key={c._id} value={c._id}>{c.name}</Option>)
            }
          </Select>
        </Form.Item>
        <Form.Item>
          <Input 
            onChange={this.changeName} 
            defaultValue={defaultAddValue}
            placeholder="请输入分类名称" 
          />
        </Form.Item>
      </Form>
    )
  }
}
